package cn.hznc.controller.service.inventory;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hznc.domain.entity.*;
import cn.hznc.domain.request.inventory.InBoundOrOutBoundReq;
import cn.hznc.domain.request.inventory.SingleWarehouseSafetyInventoryParam;
import cn.hznc.mapper.EntryExitRecordMapper;
import cn.hznc.mapper.ToolPartEntryExitRecordMapper;
import cn.hznc.mapper.ToolPartMapper;
import cn.hznc.repository.AssemblyToolInventoryRepository;
import cn.hznc.repository.InventoryLocationRespository;
import cn.hznc.repository.ToolForkInfoRespository;
import cn.hznc.repository.ToolPartRespository;
import cn.hznc.vo.PartParamVo;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author：Administrator
 * @Date：2023/5/5 23:09
 */
@Service
public class EntryExitRecordService{

    @Resource
    private EntryExitRecordMapper entryExitRecordMapper;

    @Resource
    private ToolPartEntryExitRecordMapper toolPartEntryExitRecordMapper;

    @Resource
    private InventoryLocationRespository inventoryLocationRespository;

    @Resource
    private AssemblyToolInventoryRepository assemblyToolInventoryRepository;

    @Resource
    private ToolForkInfoRespository ToolForkInfoRespository;

    @Resource
    private ToolPartRespository toolPartRespository;

    @Resource
    private ToolPartMapper toolPartMapper;
    public void createEntryRecord(ToolInventoryLocationEntity toolInventoryLocationEntity, Integer number, List<Integer> toolPartIdList , String type , String operator , String bz , String taskId , String productionNum , Date createTime){
        Map<String , Object> paramMap = inventoryLocationRespository.getWarehouseNameAndType(toolInventoryLocationEntity.getInventoryLocationId());
        EntryExitRecordEntity entryExitRecordEntity = new EntryExitRecordEntity();
        if(ObjectUtil.isNotNull(paramMap)){
            entryExitRecordEntity.setWarehouseName((String) paramMap.getOrDefault("name" , ""));
            entryExitRecordEntity.setWarehouseType((String)paramMap.getOrDefault("type" , ""));
            entryExitRecordEntity.setWarehouseId((Integer)paramMap.getOrDefault("id" , null));
        }
        entryExitRecordEntity.setCreateTime(createTime);
        entryExitRecordEntity.setNumber(number);
        entryExitRecordEntity.setOperator(operator);
        entryExitRecordEntity.setToolInventoryLocationId(toolInventoryLocationEntity.getId());
        entryExitRecordEntity.setType(type);
        entryExitRecordEntity.setBz(bz);
        entryExitRecordEntity.setUnit("个");
        //设置工单号和物料编码
        if(StringUtil.isNotEmpty(taskId) || StringUtil.isNotEmpty(productionNum)){
            entryExitRecordEntity.setTaskId(taskId);
            entryExitRecordEntity.setProductionNum(productionNum);
        }
        buildParams(entryExitRecordEntity , toolInventoryLocationEntity);
        entryExitRecordMapper.insert(entryExitRecordEntity);
        //往库存记录与单个零件关系表中插入记录
        List<ToolPartEntryExitRecordEntity> toolPartEntryExitRecordEntities = new ArrayList<>();
        toolPartIdList.forEach(toolPartId ->{
            ToolPartEntryExitRecordEntity toolPartEntryExitRecordEntity = new ToolPartEntryExitRecordEntity();
            toolPartEntryExitRecordEntity.setEntryExitRecordId(entryExitRecordEntity.getId());
            toolPartEntryExitRecordEntity.setToolPartId(toolPartId);
            toolPartEntryExitRecordEntities.add(toolPartEntryExitRecordEntity);
        });
        if(!CollectionUtil.isEmpty(toolPartEntryExitRecordEntities)){
            toolPartEntryExitRecordMapper.insertBatchSomeColumn(toolPartEntryExitRecordEntities);
        }
    }

    public void createEntryRecordReload(ToolInventoryLocationEntity toolInventoryLocationEntity, Integer number, List<Integer> toolPartIdList , String type , String operator , String bz , String taskId , String productionNum , Date createTime , String toolPartParams){
        Map<String , Object> paramMap = inventoryLocationRespository.getWarehouseNameAndType(toolInventoryLocationEntity.getInventoryLocationId());
        EntryExitRecordEntity entryExitRecordEntity = new EntryExitRecordEntity();
        if(ObjectUtil.isNotNull(paramMap)){
            entryExitRecordEntity.setWarehouseName((String) paramMap.getOrDefault("name" , ""));
            entryExitRecordEntity.setWarehouseType((String)paramMap.getOrDefault("type" , ""));
            entryExitRecordEntity.setWarehouseId((Integer)paramMap.getOrDefault("id" , null));
        }
        entryExitRecordEntity.setCreateTime(createTime);
        entryExitRecordEntity.setNumber(number);
        entryExitRecordEntity.setOperator(operator);
        entryExitRecordEntity.setToolInventoryLocationId(toolInventoryLocationEntity.getId());
        entryExitRecordEntity.setType(type);
        entryExitRecordEntity.setBz(bz);
        entryExitRecordEntity.setToolParams(toolPartParams);
        entryExitRecordEntity.setUnit("个");
        //设置工单号和物料编码
        if(StringUtil.isNotEmpty(taskId) || StringUtil.isNotEmpty(productionNum)){
            entryExitRecordEntity.setTaskId(taskId);
            entryExitRecordEntity.setProductionNum(productionNum);
        }
        buildParams(entryExitRecordEntity , toolInventoryLocationEntity);
        entryExitRecordMapper.insert(entryExitRecordEntity);
        //往库存记录与单个零件关系表中插入记录
        List<ToolPartEntryExitRecordEntity> toolPartEntryExitRecordEntities = new ArrayList<>();
        toolPartIdList.forEach(toolPartId ->{
            ToolPartEntryExitRecordEntity toolPartEntryExitRecordEntity = new ToolPartEntryExitRecordEntity();
            toolPartEntryExitRecordEntity.setEntryExitRecordId(entryExitRecordEntity.getId());
            toolPartEntryExitRecordEntity.setToolPartId(toolPartId);
            toolPartEntryExitRecordEntities.add(toolPartEntryExitRecordEntity);
        });
        if(!CollectionUtil.isEmpty(toolPartEntryExitRecordEntities)){
            toolPartEntryExitRecordMapper.insertBatchSomeColumn(toolPartEntryExitRecordEntities);
        }
    }


    private void buildParams(EntryExitRecordEntity entryExitRecordEntity, ToolInventoryLocationEntity toolInventoryLocationEntity) {
        Map<String ,Object> paramMap = new HashMap<>();
        paramMap.put("physicalInventory" , toolInventoryLocationEntity.getPhysicalInventory());
        paramMap.put("safetyInventory" , ObjectUtil.isNull(toolInventoryLocationEntity.getSafetyInventory())? 0 : toolInventoryLocationEntity.getSafetyInventory());
        ToolForkInfoEntity toolForkInfoEntity = ToolForkInfoRespository.queryPartnoById(toolInventoryLocationEntity.getToolId());
        String toolNo = assemblyToolInventoryRepository.selectToolNoById(toolInventoryLocationEntity.getAssemblyToolInventoryId());
        if(ObjectUtil.isNull(toolForkInfoEntity)){
            paramMap.put("toolno" , ObjectUtil.isNull(toolNo)? "" : toolNo);
            entryExitRecordEntity.setToolNo(ObjectUtil.isNull(toolNo)? "" : toolNo);
        }else {
            paramMap.put("partno" , ObjectUtil.isNull(toolForkInfoEntity)? "" : toolForkInfoEntity.getPartno());
            entryExitRecordEntity.setPartno(ObjectUtil.isNull(toolForkInfoEntity)? "" : toolForkInfoEntity.getPartno());
            entryExitRecordEntity.setToolTypeCode(ObjectUtil.isNull(toolForkInfoEntity)? "" : toolForkInfoEntity.getType());
        }
        entryExitRecordEntity.setParams(JSONObject.toJSONString(paramMap));
    }

    private void buildParamsReload(EntryExitRecordEntity entryExitRecordEntity, ToolInventoryLocationEntity toolInventoryLocationEntity, Integer toolPartId) {
        Map<String ,Object> paramMap = new HashMap<>();
        paramMap.put("physicalInventory" , toolInventoryLocationEntity.getPhysicalInventory());
        paramMap.put("safetyInventory" , ObjectUtil.isNull(toolInventoryLocationEntity.getSafetyInventory())? 0 : toolInventoryLocationEntity.getSafetyInventory());
        ToolForkInfoEntity toolForkInfoEntity = ToolForkInfoRespository.queryPartnoById(toolInventoryLocationEntity.getToolId());
        String toolNo = assemblyToolInventoryRepository.selectToolNoById(toolInventoryLocationEntity.getAssemblyToolInventoryId());
        if(ObjectUtil.isEmpty(toolForkInfoEntity)){
            paramMap.put("toolno" , ObjectUtil.isNull(toolNo)? "" : toolNo);
            entryExitRecordEntity.setToolNo(ObjectUtil.isNull(toolNo)? "" : toolNo);
            //transferPartParamMap(toolPartId);
        }else {
            paramMap.put("partno" , ObjectUtil.isNull(toolForkInfoEntity)? "" : toolForkInfoEntity.getPartno());
            entryExitRecordEntity.setPartno(ObjectUtil.isNull(toolForkInfoEntity)? "" : toolForkInfoEntity.getPartno());
            entryExitRecordEntity.setToolTypeCode(ObjectUtil.isNull(toolForkInfoEntity)? "" : toolForkInfoEntity.getType());
        }
        entryExitRecordEntity.setParams(JSONObject.toJSONString(paramMap));
    }

    private List<PartParamVo> transferPartParamMap(Integer toolPartId) {
        ToolPartEntity toolPartEntity = toolPartMapper.selectById(toolPartId);
        List<PartParamVo> partParamVos = new ArrayList<>();
        if(ObjectUtil.isNotNull(toolPartEntity) && ObjectUtil.isNotNull(toolPartEntity.getTypeFlag()) && toolPartEntity.getTypeFlag() == 1){
            //总成 构建参数map

        }
        return partParamVos;
    }


}
